<?php
class ecConnectionDatabase{

  protected static $dbMaster;
  protected static $dbSlave;

  public static function init()
  {
    try{
      $yaml = new sfYamlParser();

      if (($value = apc_fetch('db')) === false)
      {
        $value = $yaml->parse(file_get_contents(dirname(__FILE__).'/../../settings/db.yml'));
        apc_store('db', serialize($value), APC_TIMEOUT);
      }
      else
      {
        $value = unserialize($value);
      }
    }
    catch (InvalidArgumentException $e)
    {
      // an error occurred during parsing
      throw new Exception("Unable to parse the YAML string: ".$e->getMessage());
    }

    $env = ecEnvironment::getEnvironment();

    self::$dbMaster = new DB(
        $value[$env]['master']['dsn'],
        $value[$env]['master']['username'],
        $value[$env]['master']['password']
        );

    self::$dbSlave = new DB(
        $value[$env]['slave']['dsn'],
        $value[$env]['slave']['username'],
        $value[$env]['slave']['password']
        );
  }

  public static function getDbMaster()
  {
    return self::$dbMaster;
  }

  public static function getDbSlave()
  {
    return self::$dbSlave;
  }

}
